
#include <iostream>
#include <map>
#include <stdexcept>

using namespace std;

class Student{
public:
	Student(string const& name="", int score=0):m_name(name), m_score(score){}
private:
	string m_name;
	int m_score;
	friend ostream& operator<<(ostream& os,Student const& stu);
};

ostream& operator<<(ostream& os, Student const& stu){
	return os << stu.m_name << ' ' << stu.m_score;
}

void print(map<string, Student>& m){
	typedef map<string, Student>::iterator IT;
	for(IT it = m.begin(); it != m.end(); ++it)
		cout << (*it).first << ':' << (*it).second << endl;
	cout << "---------------------" << endl;
}

int main(){
//	 init
//	map<string, Student> m;
	map<string, Student> m{
		{"1001", {"zhangsan", 80}}, {"1006", {"wangwu", 78}}, {"1003", {"zhaoliying", 99}}
	};
	print(m);
	// Insert
	m.insert(pair<string, Student>("1009", Student("yufengguang", 100)));
	print(m);

	m.insert(make_pair("1002", Student("lufei", 0)));
	print(m);

	m.insert({{"1010", Student("suolong", 70)}, {"1004", Student("juanmao", 78)}});
	print(m);

	m["1003"] = Student("wanqian", 1);
	print(m);

	typedef map<string, Student>::iterator IT;
	IT it = m.begin();
	cout << "Using iterator to visit" << endl;
	cout << it->first << ':' << it->second << endl;
	cout << "Using key to visit" << endl;
	cout << "1005:"<< m["1005"] << endl;
	try{
		Student s = m.at("1005");
		cout << s << endl;
	}
	catch(out_of_range& e){
		cerr << e.what() << endl;
		cout << "No data" << endl;
	}

	//	erase
	if(m.erase("1005"))
		cout << "1005 has been delete" << endl;
	else
		cout << "Not exist" << endl;

	it = m.erase(--m.end());
	if(it == m.end())
		cout << "The last one has been delete" << endl;
	else
		cout << "position : " << it->first << "delete" << endl;

	IT fit = m.find("1002");
	if(fit != m.end())
		cout << "Finded : " << fit->second << endl;
	else
		cout << "1002 Not Find" << endl;

	return 0;
}
